﻿
'use strict';

define([
    'backgrid',
    'js/backgrid.overrides',
    'assets/backgrid/plugins/filter/0.3.5/js/backgrid-filter.0.3.5',
    'assets/backgrid/plugins/moment-cell/0.3.0/js/backgrid-moment-cell',
    'assets/backgrid/plugins/paginator/0.3.5/js/backgrid-paginator.0.3.5',
    'assets/backgrid/plugins/select2-cell/0.3.5/js/backgrid-select2-cell',
    'assets/backgrid/plugins/text-cell/0.3.0/js/backgrid-text-cell',
    'assets/backgrid/plugins/typeahead-cell/1.0.0/js/backgrid-typeahead-cell',
], function (Backgrid) {
    //****************
    //Formatters
    //****************
    Backgrid.Extension.CurrencyCellFormatter = function () { };
    Backgrid.Extension.CurrencyCellFormatter.prototype = new Backgrid.CellFormatter();
    _.extend(Backgrid.Extension.CurrencyCellFormatter.prototype, {
        fromRaw: function (rawData, model) {
            return '$' + parseFloat(formattedData).toFixed(2);
        },
        toRaw: function (formattedData, model) {
            return parseFloat(rawData.toString().replace('$', ''), 10).toFixed(2);
        },
    });
    Backgrid.Extension.PadCellFormatter = function () { };
    Backgrid.Extension.PadCellFormatter.prototype = new Backgrid.CellFormatter();
    _.extend(Backgrid.Extension.PadCellFormatter.prototype, {
        pad: '',

        fromRaw: function (rawData, model) {
            return (rawData) ? utils.misc.padLeft(rawData + '', this.pad) : rawData;
        },
        toRaw: function (formattedData, model) {
            return utils.misc.padLeft(formattedData + '', this.pad);
        },
    });
    //****************
    //Cells
    //****************
    Backgrid.Extension['button'] =
    Backgrid.Extension.ButtonCell = Backgrid.Cell.extend({
        buttonLabel: '',
        className: 'button-cell',
        template: '<button class="btn">{{label}}</button>',
        render: function () {
            var templateBuilder = Handlebars.compile(this.template);

            this.$el.html(templateBuilder({ label: this.buttonLabel, }));

            this.delegateEvents();

            return this;
        }
    });
    Backgrid.Extension['currency'] =
    Backgrid.Extension.CurrencyCell = Backgrid.NumberCell.extend({
        className: 'number-cell currency-cell',
        formatter: Backgrid.Extension.CurrencyCellFormatter,
    });
    Backgrid.Extension['delete-row'] =
    Backgrid.Extension.DeleteRowCell = Backgrid.Cell.extend({
        className: 'delete-cell',
        template: _.template('<i class="fa fa-times"></i>'),
        events: {
            'click': 'click'
        },
        mustDelete: function () {
            return true;
        },
        deleteRow: function () {
            if (this.mustDelete() == true)
                this.model.collection.remove(this.model);
        },
        render: function () {
            this.$el.html(this.template());
            this.delegateEvents();
            return this;
        },
        click: function (e) {
            e.preventDefault();
            this.deleteRow();
        },
    });
    Backgrid.Extension['string-center'] =
    Backgrid.Extension.StringCenterCell = Backgrid.StringCell.extend({
        className: 'string-cell string-center-cell',
    });
    Backgrid.Extension['string-left'] =
    Backgrid.Extension.StringLeftCell = Backgrid.StringCell.extend({
        className: 'string-cell string-left-cell',
    });
    Backgrid.Extension.MomentCenterCell = Backgrid.Extension.MomentCell.extend({
        className: 'moment-cell moment-center-cell',
    });
    //****************
    //Rows
    //****************
    Backgrid.Extension['conditional-style-row'] =
    Backgrid.Extension.ConditionalStyleRow = Backgrid.Row.extend({
        super: null,

        classes: {},

        constructor: function () {
            this.super = Backgrid.Extension.ConditionalStyleRow.__super__;

            this.super.constructor.apply(this, arguments);
        },

        render: function () {
            var cssClass = this.evaluate();

            this.$el.addClass(cssClass);

            return this.super.render.apply(this, arguments);
        },
        evaluate: function () { return ''; },
    });

    return Backgrid;
});